package com.hbt.myplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbt.myplus.model.Drug;
import com.hbt.myplus.model.Pay;
import com.hbt.myplus.model.Reg;
import com.hbt.myplus.service.impl.DrugServiceImpl;
import com.hbt.myplus.service.impl.PayServiceImpl;
import com.hbt.myplus.service.impl.RegServiceImpl;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.hbt.myplus.service.IRegDrugService;
import com.hbt.myplus.service.impl.RegDrugServiceImpl;
import com.hbt.myplus.model.RegDrug;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hbt.myplus.utils.ResponseWeb;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
    *  前端控制器
    * </p>
 *
 * @author astupidcoder
 * @since 2020-12-30
 * @version v1.0
 */
@RestController
@RequestMapping("reg/drug")


public class RegDrugController {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private RegDrugServiceImpl regDrugService;
    @Autowired
    private DrugServiceImpl drugService;
    @Autowired
    private RegServiceImpl regService;
    @Autowired
    private PayServiceImpl payService;

    /**
     * 查询分页数据
     */
    @GetMapping(value = "/s")
    public ResponseWeb<Page> findListByPage(@RequestParam(name = "pageNum", defaultValue = "1") int pageNum,@RequestParam(name = "pageSize", defaultValue = "20") int pageSize){
        return null;
    }

    /**
     * 根据regid查询
     */
    @GetMapping(value = "/list_by_reg_no")
    public ResponseWeb listByRegNo(@RequestParam("regNo") String regNo){
        if (regNo == null || regNo.equals("")){
            return new ResponseWeb(new ArrayList(0));
        }
        Reg reg = regService.getOne(new QueryWrapper<Reg>().eq("reg_no", regNo));
        if (reg == null){
            return new ResponseWeb(new ArrayList(0));
        }
        List<RegDrug> regDrugs = regDrugService.list(new QueryWrapper<RegDrug>().eq("reg_id", reg.getId()));
        if (regDrugs.size() == 0){
            return new ResponseWeb(new ArrayList(0));
        }
        List<Integer> drugIds = regDrugs.stream().map(a -> a.getDrugId()).collect(Collectors.toList());
        List<Drug> drugs = (List<Drug>) drugService.listByIds(drugIds);
        regDrugs.forEach(a -> {
            Drug drug = drugs.stream().filter(b -> b.getId().equals(a.getDrugId())).collect(Collectors.toList()).get(0);
            a.setDrug(drug);
            int totalPrice = Integer.parseInt(drug.getPrice().toString())*a.getCount();
            a.setTotalPrice(totalPrice);
        });

        return new ResponseWeb(regDrugs);
    }

    /**
     * 根据id查询
     */
    @GetMapping(value = "/{id}")
    public ResponseWeb<RegDrug> getById(@PathVariable("id") String id){
       return null;
    }

    /**
     * 新增
     */
    @PostMapping
    public ResponseWeb<RegDrug> add(@RequestBody RegDrug regDrug){
        return null;
    }

    /**
     * 删除
     */
    @DeleteMapping
    public ResponseWeb<String> delete(@RequestBody List<String> ids){

        return new ResponseWeb(regDrugService.removeByIds(ids));
    }

//    @DeleteMapping
//    public ResponseWeb<String> delete(@RequestBody RegDrug regDrug){
//
//        return new ResponseWeb(regDrugService.getBaseMapper().delete(new QueryWrapper<RegDrug>().eq("reg_id", regDrug.getRegId()).eq("drug_id", regDrug.getDrugId())));
//    }

    /**
     * 修改
     */
    @GetMapping(value = "/pay/{id}")
    public ResponseWeb setPay(@PathVariable("id") int id, @RequestHeader("openid") String openid){
        //设置为已经支付
        RegDrug regDrug = new RegDrug();
        regDrug.setId(id);
        regDrug.setIsPay(1);
        regDrugService.updateById(regDrug);
        //增加缴费记录
        RegDrug rd = regDrugService.getById(id);

        Pay pay = new Pay();
        pay.setItemname("药物缴费");
        pay.setPoid(openid);
        pay.setPrice(drugService.getById(rd.getDrugId()).getPrice().multiply(new BigDecimal(rd.getCount())));
        pay.setSno(UUID.randomUUID().toString());
        pay.setTime(new Date());
        payService.save(pay);
        return new ResponseWeb("支付成功");
    }

    /**
     * 修改
     */
    @GetMapping(value = "/send/{id}")
    public ResponseWeb setSend(@PathVariable("id") int id){
        RegDrug regDrug = new RegDrug();
        regDrug.setId(id);
        regDrug.setIsSend(1);
        regDrugService.updateById(regDrug);
        return new ResponseWeb("发药成功");
    }

    /**
     * 修改
     */
    @PutMapping
    public ResponseWeb<RegDrug> update(@RequestBody RegDrug regDrug){
        return null;
     }

}
